package serial

import (
	"slgw_web/serial/pac"
	"slgw_web/serial/util"
	"strconv"
	"time"

	models "openzbox.com/frame/echo/models/sl_gw01"
)

// var count int

func SerialStart() {
	go func() {
		Project()
	}()
	//监听楼口数据
	// go func() {
	// 	pac.LouKouTCPConnect()
	// }()
}
func Project() {
	//count++
	//fmt.Println("进入次数", count)
	// 连接到服务端建立的tcp连接
	defer func() {
		if err := recover(); err != nil {
			util.Error("抛出异常:", err)
		}
	}()
	//开启一个定时采集任务
	pac.CollectNew()
	pac.RTUClient()
	pac.SiemensClient()

	var sb models.DeviceInfo
	for i := range pac.Test {
		t := pac.Test[i]
		pac.TaskCollectData(t) //先直接采集一次
		pac.CreateTimer(sb.DeviceInfoID+util.Task3+strconv.Itoa(int(t.DataID)), time.Duration(t.TPITV)*time.Second, "", 1, sb, &t, nil)

	}
	//开启定时采集数据推送任务
	pac.CreateTimer(sb.DeviceInfoID+util.Task5, 5*time.Second, "", 5, sb, nil, nil)
	//定时推送网关时间
	pac.CreateTimer(sb.DeviceInfoID+util.Task6, 1*time.Second, "", 6, sb, nil, nil)
	//添加采集缓存到数据库
	pac.CreateTimer(sb.DeviceInfoID+util.Task7, 10*time.Minute, "", 7, sb, nil, nil)
	//定时自动校时
	pac.TaskSetTime()
	pac.CreateTimer(util.Task10, 1*time.Hour, "", 12, sb, nil, nil)
	pac.TcpConnect()

}
